Skip to main content

MongoDB - Text Search

Starting from version 2.4, MongoDB supports text indexes for searching inside string content. Text Search uses stemming techniques to search for specified words in string fields by dropping stemming stop words like "a," "an," "the," etc. MongoDB currently supports around 15 languages for text search.

Initially, Text Search was an experimental feature, but starting from version 2.6, it is enabled by default in MongoDB configurations.

Creating Text Index​

Consider the following documents in the posts collection:

db.posts.insert({
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": ["mongodb", "tutorialspoint"]
})

db.posts.insert({
"post_text": "writing tutorials on mongodb",
"tags": ["mongodb", "tutorial"]
})

To create a text index on the post_text field, enabling text search inside the posts' text:

db.posts.createIndex({ post_text: "text" })

Using Text Index​

After creating the text index on the post_text field, you can search for posts containing specific words using the $text operator:

db.posts.find({ $text: { $search: "tutorialspoint" } }).pretty()

This command retrieves documents that contain the word "tutorialspoint" in their post text.

Diagram: Text Search Process​

::: note: Performance Considerations

When using text search in MongoDB:

  • Text indexes can significantly improve search performance for string content.
  • Consider the language-specific settings for stemming and stop words.
  • Optimize text indexes based on the search requirements. :::

Table: Text Search Options​

OptionDescription
$textOperator for text search using text indexes.
$searchSpecifies the search term or phrase.
$languageOptional parameter for specifying the language for text search.

Deleting Text Index​

To delete an existing text index, first, find the name of the index using the getIndexes method:

db.posts.getIndexes()

After obtaining the index name, use the dropIndex method to delete the text index:

db.posts.dropIndex("post_text_text")

This command deletes the text index named "post_text_text" from the posts collection.